/*
 * Copyright (c) 2010 - 2011, Brookhaven National Laboratory
 * All rights reserved.
 * The software in this package is published under the terms
 * of the BSD license a copy of which has been included with
 * this distribution in the license.txt file.
 */

package org.oxtypes.impl;

import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

import org.omg.dds.type.dynamic.DynamicType;
import org.omg.dds.type.dynamic.DynamicTypeMember;
import org.omg.dds.type.dynamic.MemberDescriptor;

import org.oxtypes.DynamicData;
import org.oxtypes.DynamicDataFactory;

/**
 *
 */
public class StructureImpl extends AbstractDynamicData {
    
    private DynamicType type;
    private Map<Integer, DynamicData> members;
    private List<MemberDescriptor> descriptors;

    public StructureImpl(DynamicType type){
      this.type = type;      
      this.members = new TreeMap<Integer, DynamicData>();
      this.descriptors = new LinkedList<MemberDescriptor>();
    }

    public DynamicType getType(){
        return type;
    }

    public List<MemberDescriptor> getDescriptors(){
        return java.util.Collections.unmodifiableList(descriptors);
    }

    public int getMemberIdByName(String name){
        DynamicTypeMember dtm = type.getMember(name);
        if(dtm == null) return -1;
        return dtm.getId();
    }

    /** Unsupported */
    public int getMemberIdAtIndex(int index){
        throw new java.lang.UnsupportedOperationException();
    }

    public void clearAllValues(){
        descriptors.clear();
        members.clear();
    }

    public void clearValue(int id){
        descriptors.remove(type.getMemberById(id).getDescriptor());
        members.remove(id);
    }

    public double getFloat64Value(int id){
        
        DynamicData dd = members.get(id);
        if(dd != null) return dd.getFloat64Value(0);

        DynamicTypeMember dtm = type.getMemberById(id);
        if(dtm == null) throw new java.lang.UnsupportedOperationException();

        // dtm.getDescriptor().getDefaultValue();
        return 0.0;
    }

    public DynamicData setFloat64Value(int id, double value){
        DynamicData dd = members.get(id);
        if(dd != null) return dd.setFloat64Value(0, value);

        DynamicTypeMember dtm = type.getMemberById(id);
        if(dtm == null) throw new java.lang.UnsupportedOperationException();

        DynamicDataFactory ddf = DynamicDataFactory.getInstance();

        dd = ddf.createData(dtm.getDescriptor().getType());
        dd.setFloat64Value(0, value);

        members.put(id, dd);
        descriptors.add(dtm.getDescriptor());

        return this;
    }

    public int getFloat64Values(
            double[] value, int offset, int length, int id){

        DynamicData dd = members.get(id);
        if(dd != null) return dd.getFloat64Values(value, offset, length, 0);

        DynamicTypeMember dtm = type.getMemberById(id);
        if(dtm == null) throw new java.lang.UnsupportedOperationException();

        return 0; // ?
    }

    public DynamicData setFloat64Values(
            int id, double[] value, int offset, int length){

        DynamicData dd = members.get(id);
        if(dd != null) return dd.setFloat64Values(0, value, offset, length);

        DynamicTypeMember dtm = type.getMemberById(id);
        if(dtm == null) throw new java.lang.UnsupportedOperationException();

        DynamicDataFactory ddf = DynamicDataFactory.getInstance();

        dd = ddf.createData(dtm.getDescriptor().getType());
        dd.setFloat64Values(0, value, offset, length);

        members.put(id, dd);
        descriptors.add(dtm.getDescriptor());

        return this;
    }

    /** Unsupported */
    public DynamicData setFloat64Values(int id, double... value){
        throw new java.lang.UnsupportedOperationException();
    }

    public DynamicData getComplexValue(int id){
        return members.get(id);
    }

    public DynamicData setComplexValue(int id, DynamicData value){
        throw new java.lang.UnsupportedOperationException();
    }

}
